package com.nlelpct.manage.controller;

import com.nlelpct.manage.model.dto.*;
import com.nlelpct.manage.model.entity.SysEmployee;
import com.nlelpct.manage.model.query.InsertAndUpdateEmployeeQuery;
import com.nlelpct.manage.model.query.SysEmployeeListByPageQuery;
import com.nlelpct.manage.model.vo.*;
import com.nlelpct.manage.service.ISysEmployeeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 刘伟涛
 * @Date 2023/7/18 16:37
 **/
@RestController
@RequestMapping("/sysEmployee")
public class SysEmployeeController {
    @Resource
    private ISysEmployeeService sysEmployeeService;

    /**
     * 判断手机号能否添加员工
     */
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/getWhetherAdd")
    public ResponseResult<String> getWhetherAdd(@RequestBody SysEmployeeWhetherAddDto sysEmployeeWhetherAddDto) {
        return sysEmployeeService.getWhetherAdd(sysEmployeeWhetherAddDto);
    }

    /**
     * 添加员工-更新员工时判断员工工号是否重复
     */
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/validateEmployeeNumber")
    public ResponseResult<Boolean> validateEmployeeNumber(@RequestBody InsertAndUpdateEmployeeQuery validateEmployeeQuery) {
        return sysEmployeeService.getEmployeeNumberWhetherExist(validateEmployeeQuery);
    }

    /**
     * 添加员工
     */
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/addEmployee")
    public ResponseResult<String> addEmployee(@RequestBody InsertAndUpdateEmployeeQuery insertEmployeeQuery){
        return sysEmployeeService.addEmployee(insertEmployeeQuery);
    }

    /**
     * 分页查询
     */
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/getAllByPage")
    public ResponseResult<PageResultVo<SysEmployeeListByPageVo>> getAllByPage(@RequestBody SysEmployeeListByPageQuery sysEmployeeListByPageQuery){
        return sysEmployeeService.getAllByPage(sysEmployeeListByPageQuery);
    }

    /**
     * 修改员工禁用状态
     */
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/updateEmployeeStatus/{employeeId}")
    public ResponseResult<String> updateEmployeeStatus(@PathVariable(value = "employeeId")String employeeId) {
        return sysEmployeeService.updateEmployeeStatus(employeeId);
    }

    /**
     * 单查员工信息
     */
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/getEmployeeBaseInfo/{employeeId}")
    public ResponseResult<InsertAndUpdateEmployeeQuery> getEmployeeBaseInfo(@PathVariable(value = "employeeId")String employeeId) {
        return sysEmployeeService.getEmployeeBaseInfo(employeeId);
    }

    /**
     * 修改员工信息
     */
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/updateEmployeeBaseInfo")
    public ResponseResult<String> updateEmployeeBaseInfo(@RequestBody InsertAndUpdateEmployeeQuery updateEmployeeQuery){
        return sysEmployeeService.updateEmployeeBaseInfo(updateEmployeeQuery);
    }

    /**
     * 重置密码
     */
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/updateEmployeePassword/{employeeId}")
    public ResponseResult<String> updateEmployeePassword(@PathVariable(value = "employeeId")String employeeId) {
        return sysEmployeeService.updateEmployeePassword(employeeId);
    }

    /**
     * 获取部门分级列表
     */
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/getDeptList")
    public ResponseResult<List<SysDeptListVo>> getDeptList() {
        return sysEmployeeService.getDeptList();
    }

    /**
     * 获取角色列表
     */
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/getRoleList/{employeeId}")
    public ResponseResult<List<SysRoleListVo>> getRoleList(@PathVariable(value = "employeeId")String employeeId) {
        return sysEmployeeService.getRoleList(employeeId);
    }

    /**
     * 配置员工角色关系
     */
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/updateEmployeeAndRole")
    public ResponseResult<String> updateEmployeeAndRole(@RequestBody SysEmployeeAndRoleUpdateDto sysEmployeeAndRoleUpdateDto){
        return sysEmployeeService.updateEmployeeAndRole(sysEmployeeAndRoleUpdateDto);
    }

    /**
     * 获取岗位列表
     */
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/getPostList")
    public ResponseResult<List<SysPostListVo>> getPostList(){
        return sysEmployeeService.getPostList();
    }

    /**
     * 导出员工信息列表
     */
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/exportEmployeeInfo")
    public void exportEmployeeInfo(@RequestBody List<String> ids , HttpServletResponse response){
        sysEmployeeService.exportEmployeeInfo(ids, response);
    }

    /**
     * 查询操作人员
     */
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/getSysEmployeeList")
    public ResponseResult<List<SysEmployee>> getSysEmployeeList(){
        return sysEmployeeService.getSysEmployeeList();
    }

    @PreAuthorize("hasRole('admin')")
    @PostMapping("/getSysEmployeeListByDeptIds")
    public ResponseResult<List<SysEmployee>> getSysEmployeeListByDeptIds(@RequestBody List<String> deptIds){
        return sysEmployeeService.getSysEmployeeListByDeptIds(deptIds);
    }
}
